package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Together;
import cn.wolfcode.trip.base.query.TogetherQueryObject;
import cn.wolfcode.trip.base.service.ITogetherService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/togethers")
public class TogetherController {

    @Autowired
    private ITogetherService togetherService;

    @PostMapping
    public JsonResult save(Together together) {
        JsonResult result = new JsonResult();
        try {
            together.setAuthor(UserContext.getCurrentUser());
            togetherService.save(together);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @PutMapping
    public JsonResult update(Together together) {
        JsonResult result = new JsonResult();
        try {
            togetherService.update(together);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("{id}")
    public Together get(@PathVariable Long id) {

        return togetherService.get(id);
    }

    /**
     * 查询所有已发布的结伴
     *
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo<Together> listAllReleaseTogether(TogetherQueryObject qo) {
        //设置已发布状态的高级查询条件
        qo.setState(2);
        qo.setOrderBy("t.lastUpdateTime DESC");
        return togetherService.queryForList(qo);
    }



}
